﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

namespace BaoJianSoft.ExtensionModel.Web
{
	using System;
	using System.Collections.Generic;
	using System.Globalization;
	using System.Linq;
	using System.Text;
	using System.Web.UI;
	using BaoJianSoft.ExtensionModel.Web.Controls;
	using BaoJianSoft.RapidWeb;
	using BaoJianSoft.RapidWeb.Controls;
	using BaoJianSoft.RapidWeb.DynamicPages;
	using WebControls = System.Web.UI.WebControls;

	/// <summary>
	/// The implementation class to build control for Selection typed extension field.
	/// </summary>
	public class EnumerationExtensionFieldControlBuilder : AbstractExtensionFieldControlBuilder
	{
		private ComboBox comboBox;

		[Binding]
		protected TextBox TextBoxName;
		[Binding]
		protected CheckBox CheckBoxRequired;
		[Binding]
		protected IntegerTextBox IntegerTextBoxOrdinal;
		[Binding]
		protected SelectionExtensionFieldControl SelectionExtensionFieldControl;
		[Binding]
		protected TextBox TextBoxDescription;

		/// <summary>
		/// Sets/gets editor control value.
		/// </summary>
		public override object Value
		{
			get { return new EnumerationValueCollection { this.comboBox.SelectedValue }; }
			set 
			{
				EnumerationValueCollection selectionCollection = value as EnumerationValueCollection;
				this.comboBox.SelectedValue = selectionCollection != null && selectionCollection.Count > 0 ? selectionCollection[0] : "";
			}
		}

		/// <summary>
		/// Sets/gets field metadata in metadata management UI.
		/// </summary>
		public override IFieldMetadata Metadata
		{
			get
			{
				EnumerationFieldMetadata metadata = new EnumerationFieldMetadata
				{
					Name = this.TextBoxName.Text,
					IsRequired = this.CheckBoxRequired.Checked,
					Ordinal = this.IntegerTextBoxOrdinal.Value.HasValue ? this.IntegerTextBoxOrdinal.Value.Value : (int)short.MaxValue,
					Description = this.TextBoxDescription.Text,
				};

				if (this.SelectionExtensionFieldControl.SelectionItems != null)
					metadata.Items = this.SelectionExtensionFieldControl.SelectionItems.Select(x => new ExtensionModel.SelectionItem { Name = x.Text, Value = x.Text, Selected = x.Selected }).ToArray();

				return metadata;
			}
			set
			{
				EnumerationFieldMetadata metadata = value as EnumerationFieldMetadata;
				if (metadata == null) return;

				this.TextBoxName.Text = metadata.Name;
				this.TextBoxName.ReadOnly = true;

				this.CheckBoxRequired.Checked = metadata.IsRequired;
				this.IntegerTextBoxOrdinal.Value = metadata.Ordinal;
				this.TextBoxDescription.Text = metadata.Description;

				if (metadata.Items != null)
					this.SelectionExtensionFieldControl.SelectionItems = metadata.Items.Select(x => new SelectionItem { Text = x.Name, Selected = x.Selected });
			}
		}

		/// <summary>
		/// Build data input control for specified field metadata.
		/// </summary>
		/// <param name="fieldMetadata"></param>
		/// <returns></returns>
		public override ExtensionDataInputControl BuildDataInputControl(IFieldMetadata fieldMetadata)
		{
			EnumerationFieldMetadata metadata = fieldMetadata as EnumerationFieldMetadata;
			if (metadata.SelectionMode != SelectionModes.Single)
				throw new NotImplementedException("Multiple selection field metadata is not supported.");

			WebControls.PlaceHolder placeHolder = new WebControls.PlaceHolder();

			string comboBoxId = string.Format(CultureInfo.InvariantCulture, "SLTFM{0}_{1}", WebUtility.ConvertControlId(metadata.Name), metadata.Ordinal > 0 ? metadata.Ordinal : int.MaxValue);
			this.comboBox = new ComboBox { ID = comboBoxId, Editable = false, ForceSelection = true, Mode = ComboBoxDataSourceModes.Local, Width = 154 };
			foreach (ExtensionModel.SelectionItem selectionItem in metadata.Items)
				this.comboBox.Items.Add(new WebControls.ListItem(selectionItem.Name, selectionItem.Value) { Selected = selectionItem.Selected });

			placeHolder.Controls.Add(this.comboBox);

			if (metadata.IsRequired)
			{
				LiteralControl requiredLabel = new LiteralControl("<span class=\"required\">*</span>");
				placeHolder.Controls.Add(requiredLabel);
			}

			return new ExtensionDataInputControl { Control = placeHolder, OccupiedControlCells = 1 };
		}

		/// <summary>
		/// Build a control to manage the field metadata.
		/// </summary>
		/// <returns></returns>
		public override Control BuildMetadataControl()
		{
			return base.CreateFieldMetadataTemplateControl(FieldType.Enumeration);
		}
	}
}
